<!-- 弹窗 详情 -->
<script lang="tsx" setup>
import { ElDatePicker, ElSelectV2 } from "element-plus";

import type { FormDescChild } from "@/components/form-desc/form-desc.vue";
import { EQP_CHECK_TYPE_OPTIONS } from "@/config/enum-options";
import type { WhManagerBsTbEqpCheckSaveOrUpdateUsingPostBody } from "@/genapi/wh";
import { whManagerBsTbEqpCheckSaveOrUpdateUsingPost } from "@/genapi/wh";
import { useAsync } from "@/hooks/use-async";
import { useCompanyStore } from "@/stores/company";

import { injectState } from "../state";

defineOptions({ name: "DetailDialog" });

const companyStore = useCompanyStore();

const { page } = injectState()!;

const dialogRef = shallowRef<InstanceType<GlobalComponents["PromiseDialog"]>>();
const dialogType = ref<"add" | "edit" | "detail" | string>();

const titleMap: Record<string, string> = {
	add: "新增",
	edit: "编辑",
	detail: "详情",
};

const beforeClose = async () => {
	if (dialogType.value !== "detail") {
		await ElMessageBox.confirm("确认关闭？");
	}
};

const trigger = async (type = "add", value = {} as any) => {
  console.log(value)
	dialogType.value = type;
	model.value = JSON.parse(JSON.stringify(value)) as any;
	await dialogRef.value?.open();
};

defineExpose({
	trigger,
});

const model = ref<WhManagerBsTbEqpCheckSaveOrUpdateUsingPostBody>({} as any);

const { execute, isLoading } = useAsync(async () => {
	await formDescRef.value?.formRef?.validate();
	await ElMessageBox.confirm("确认提交？", "提示", {
		type: "warning",
	});
	await whManagerBsTbEqpCheckSaveOrUpdateUsingPost({
		data: {
			...model.value,
			enterpriseId: companyStore.currentCompanyId,
		},
	});
	page.execute();

	ElMessage.success("提交成功");
	dialogRef.value?.complete();
});
const formDescRef = shallowRef<InstanceType<GlobalComponents["FormDesc"]>>();


// watchEffect(() => {
// 	model.value.checkAddress = JSON.stringify(location.value);
// });

const children: FormDescChild[] = [
	{
		label: "检修类别",
		prop: "type",
		rule: [{ required: true, message: "请选择检修类别", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<ElSelectV2
				options={[...EQP_CHECK_TYPE_OPTIONS]}
				v-model={model.value.type}
				clearable
				placeholder="请选择检修类别"
			/>
		),
		descItem: () => model.value.type || "-",
	},
	{
		label: "检修地点",
		prop: "checkAddress",
		rule: [{ required: true, message: "请输入检修地点", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<div>
				<el-input
					v-model={model.value.checkAddress}
					clearable
          class="w-175%!"
					placeholder="请输入"
					style="width:50%;"
				/>
			</div>
		),
		descItem: () => model.value.checkAddress || "-",
	},
	{
		label: "检修开始时间",
		prop: "checkTimeStart",
		rule: [{ required: true, message: "请选择检修开始时间", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<ElDatePicker
				type="datetime"
				class="w-100%!"
				valueFormat="YYYY-MM-DD HH:mm:ss"
				v-model={model.value.checkTimeStart}
				clearable
				placeholder="请选择检修开始时间"
			/>
		),
		descItem: () => model.value.checkTimeStart || "-",
	},
	{
		label: "检修结束时间",
		prop: "checkTimeEnd",
		rule: [{ required: true, message: "请选择检修结束时间", trigger: "blur" }],
		span: 1,
		formItem: () => (
			<ElDatePicker
				type="datetime"
				class="w-100%!"
				valueFormat="YYYY-MM-DD HH:mm:ss"
				v-model={model.value.checkTimeEnd}
				clearable
				placeholder="请选择检修结束时间"
			/>
		),
		descItem: () => model.value.checkTimeEnd || "-",
	},
	{
		label: "检修方案",
		prop: "checkPlan",
		rule: [{ required: true, message: "请输入检修方案", trigger: "blur" }],
		span: 2,
		formItem: () => (
			<ElInput
				v-model={model.value.checkPlan}
				type="textarea"
				rows={4}
				clearable
				placeholder="请输入检修方案"
			/>
		),
		descItem: () => model.value.checkPlan || "-",
	},
];
</script>

<template>
	<PromiseDialog
		ref="dialogRef"
		:title="titleMap[dialogType!]"
		:before-close="beforeClose"
		max-h="884px!"
		w="707px!"
	>
		<el-scrollbar>
			<FormDesc
				ref="formDescRef"
				:type="dialogType === 'detail' ? 'desc' : 'form'"
				form-class="gap-x-20px"
				:model="model"
				:children="children"
				:column="2"
			/>
		</el-scrollbar>
		<template v-if="dialogType !== 'detail'" #footer>
			<el-button @click="dialogRef?.close()">取消</el-button>
			<el-button type="primary" :is-loading="isLoading" @click="execute()"
				>确认</el-button
			>
		</template>
	</PromiseDialog>
</template>
